﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;

namespace Bus_proj.website
{
    public partial class regis_user : System.Web.UI.Page
    {
        int z;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 2)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }
        }

        public DateTime ConvestToDate(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                return new DateTime(years, mount, day);
            }
            catch (Exception)
            {

            }
            return new DateTime(years, mount, day);
        }

        public string ConvestToDate_pass(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years < 2300)
                {
                    years = years + 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                if (mount2.Length == 1)
                {
                    mount2 = "0" + mount2;
                }
                if (day2.Length == 1)
                {
                    day2 = "0" + day2;
                }
                return (day2 + mount2 + years2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            if (mount3.Length == 1)
            {
                mount3 = "0" + mount3;
            }
            if (day3.Length == 1)
            {
                day3 = "0" + day3;
            }
            return (day3 + mount3 + years3);
        }

        public static string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            try
            {
                Select SData = new Select();
                Table_Passenger x = new Table_Passenger();
                z = SData.GetId();
                x.Passenger_id = z;
                x.Passenger_prefix = DropDownList1.SelectedItem.ToString();
                x.Passenger_name = TextBox1.Text;
                x.Passenger_surname = TextBox2.Text;
                x.Passenger_identification = TextBox3.Text;
                x.Passenger_sex = DropDownList2.SelectedItem.ToString();
                DateTime ddate = ConvestToDate(TextBox4.Text);
                x.Passenger_birthday = ddate;
                x.Passenger_home_no = TextBox5.Text;
                x.Passenger_village_no = TextBox6.Text;
                x.Passenger_lane = TextBox12.Text;
                x.Passenger_road = TextBox7.Text;
                x.Passenger_district = Int32.Parse(DropDownList3.SelectedValue);
                x.Passenger_sub_district = Int32.Parse(DropDownList4.SelectedValue);
                x.Passenger_province = Int32.Parse(DropDownList5.SelectedValue);
                x.Passenger_post = TextBox8.Text;
                x.Passenger_tel = TextBox11.Text;
                x.Passenger_mail = TextBox9.Text;
                x.Access_id = 1;
                x.Category_id = 2;
                string pass = ConvestToDate_pass(TextBox4.Text);                
                x.password = getMd5Hash(pass);
                db.Table_Passengers.InsertOnSubmit(x);
                db.SubmitChanges();
                //ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มข้อมูลลูกค้าแล้ว');", true);
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มข้อมูลลูกค้าเสร็จสิ้น');", true);
                //Response.Write(@"<script language='javascript'>alert('เพิ่มข้อมูลลูกค้าแล้ว');</script>");
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มข้อมูลลูกค้าได้');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเพิ่มข้อมูลลูกค้าได้');</script>");
                throw;
            }
            //Response.Redirect("regis_user.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("regis_user.aspx");
        }

        protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList4.Items.Clear();
            DropDownList3.Items.Clear();
        }

        protected void DropDownList4_SelectedIndexChanged1(object sender, EventArgs e)
        {
            DropDownList3.Items.Clear();
        }      
        
    }
}